package service.applycheck.impl;

import dao.LcBankDao;
import java.util.ArrayList;
import java.util.List;

import model.company.Companybasic;
import model.company.Companypledger;
import model.credit.Applycustomrelation;
import model.credit.Lcbank;
import model.personal.Personalbasic;

import dao.ApplycustomrelationDao;
import dao.CompanybasicDao;
import dao.CompanymanageDao;
import dao.CompanypledgerDao;
import dao.PersonalbasicDao;

import service.applycheck.ApplyCustomRelationSearchService;
import vo.ApplyRelationBean;
import vo.CreditEnsurePeopleBean;
import vo.CreditHypothecateThingBean;
import vo.CreditPledgeThingBean;

public class ApplyCustomRelationSearchServiceImpl implements
		ApplyCustomRelationSearchService {
	private ApplycustomrelationDao applycustomrelationDao;
	private CompanybasicDao companybasicDao;
	private PersonalbasicDao personalbasicDao;
	private CompanypledgerDao companypledgerDao;
	private CompanymanageDao companymanageDao;

	private LcBankDao LcBankDao;

	@Override
	public List<?> getEnssuerList(Integer applyid) {
		List<ApplyRelationBean> applyRelationBeanList = new ArrayList<ApplyRelationBean>();

		List<Applycustomrelation> listR = this.applycustomrelationDao
				.findModel("relationtype = 3 and applyid = " + applyid);
		if (listR != null) {
			for (Applycustomrelation _applycustomrelation : listR) {
				ApplyRelationBean applyRelationBean = new ApplyRelationBean();
				if (_applycustomrelation.getRelationpeopletype() == 1) {
					applyRelationBean
							.setRelationPeopleName(this.personalbasicDao
									.findOne(
											"id",
											_applycustomrelation
													.getRelationpeopleid())
									.getName());
					List<Lcbank> lcBankList = this.LcBankDao.findModel("cid = "
							+ _applycustomrelation.getRelationpeopleid()
							+ " and type = 1");
					if (lcBankList != null) {
						applyRelationBean.setBasicBank(lcBankList.get(0)
								.getBankname());
						applyRelationBean.setForeginBank(lcBankList.get(0)
								.getSaveaccount());
					}
				} else if (_applycustomrelation.getRelationpeopletype() == 2) {
					applyRelationBean
							.setRelationPeopleName(this.companybasicDao
									.findOne(
											"id",
											_applycustomrelation
													.getRelationpeopleid())
									.getName());
					List<Lcbank> lcBankList = this.LcBankDao.findModel("cid = "
							+ _applycustomrelation.getRelationpeopleid()
							+ " and type = 2");
					if (lcBankList != null) {
						applyRelationBean.setBasicBank(lcBankList.get(0)
								.getBankname());
						applyRelationBean.setForeginBank(lcBankList.get(0)
								.getSaveaccount());
						
					}
				}
				applyRelationBeanList.add(applyRelationBean);
			}
		}
		return applyRelationBeanList.size() == 0 ? null : applyRelationBeanList;
	}

	@Override
	public List<?> getHypothecateList(Integer applyid) {
		List<ApplyRelationBean> applyRelationBeanList = new ArrayList<ApplyRelationBean>();

		List<Applycustomrelation> listR = this.applycustomrelationDao
				.findModel("relationtype = 1 and applyid = " + applyid);
		if (listR != null) {
			for (Applycustomrelation _applycustomrelation : listR) {
				ApplyRelationBean applyRelationBean = new ApplyRelationBean();
				Companypledger _companypledger = companypledgerDao.findOne(
						"id", _applycustomrelation.getThingsid());
				if (_companypledger.getTypecustom() == 1) {
					applyRelationBean
							.setRelationPeopleName(this.personalbasicDao
									.findOne("id", _companypledger.getCid())
									.getName());
				} else if (_companypledger.getTypecustom() == 2) {
					applyRelationBean
							.setRelationPeopleName(this.companybasicDao
									.findOne("id", _companypledger.getCid())
									.getName());
				}
				if (_companypledger.getCollateralvalueidentification() != null) {
					applyRelationBean.setPrice(_companypledger
							.getCollateralvalueidentification());
				}
				applyRelationBean.setThingName(_companypledger
						.getCollateralname());
				applyRelationBeanList.add(applyRelationBean);
			}
		}
		return applyRelationBeanList.size() == 0 ? null : applyRelationBeanList;
	}

	@Override
	public List<?> getPledgeList(Integer applyid) {
		List<ApplyRelationBean> applyRelationBeanList = new ArrayList<ApplyRelationBean>();

		List<Applycustomrelation> listR = this.applycustomrelationDao
				.findModel("relationtype = 2 and applyid = " + applyid);
		if (listR != null) {
			for (Applycustomrelation _applycustomrelation : listR) {
				ApplyRelationBean applyRelationBean = new ApplyRelationBean();
				Companypledger _companypledger = companypledgerDao.findOne(
						"id", _applycustomrelation.getThingsid());
				if (_companypledger.getTypecustom() == 1) {
					applyRelationBean
							.setRelationPeopleName(this.personalbasicDao
									.findOne("id", _companypledger.getCid())
									.getName());
				} else if (_companypledger.getTypecustom() == 2) {
					applyRelationBean
							.setRelationPeopleName(this.companybasicDao
									.findOne("id", _companypledger.getCid())
									.getName());
				}
				if (_companypledger.getCollateralvalueidentification() != null) {
					applyRelationBean.setPrice(_companypledger
							.getCollateralvalueidentification());
				}
				applyRelationBean.setThingName(_companypledger
						.getCollateralname());
				applyRelationBeanList.add(applyRelationBean);
			}
		}
		return applyRelationBeanList.size() == 0 ? null : applyRelationBeanList;
	}

	public ApplycustomrelationDao getApplycustomrelationDao() {
		return applycustomrelationDao;
	}

	public void setApplycustomrelationDao(
			ApplycustomrelationDao applycustomrelationDao) {
		this.applycustomrelationDao = applycustomrelationDao;
	}

	public CompanybasicDao getCompanybasicDao() {
		return companybasicDao;
	}

	public void setCompanybasicDao(CompanybasicDao companybasicDao) {
		this.companybasicDao = companybasicDao;
	}

	public PersonalbasicDao getPersonalbasicDao() {
		return personalbasicDao;
	}

	public void setPersonalbasicDao(PersonalbasicDao personalbasicDao) {
		this.personalbasicDao = personalbasicDao;
	}

	public CompanypledgerDao getCompanypledgerDao() {
		return companypledgerDao;
	}

	public void setCompanypledgerDao(CompanypledgerDao companypledgerDao) {
		this.companypledgerDao = companypledgerDao;
	}

	public CompanymanageDao getCompanymanageDao() {
		return companymanageDao;
	}

	public void setCompanymanageDao(CompanymanageDao companymanageDao) {
		this.companymanageDao = companymanageDao;
	}

	public LcBankDao getLcBankDao() {
		return LcBankDao;
	}

	public void setLcBankDao(LcBankDao lcBankDao) {
		LcBankDao = lcBankDao;
	}

	@Override
	public List<?> getAllEnssuerList(Integer applyid) {
		List<CreditEnsurePeopleBean> list = new ArrayList<CreditEnsurePeopleBean>();
		List<Applycustomrelation> lists = this.applycustomrelationDao
				.findModel("applyid = " + applyid + " and relationtype = 3");
		if (lists != null) {
			for (Applycustomrelation acl : lists) {
				CreditEnsurePeopleBean cepb = new CreditEnsurePeopleBean();
				cepb.setPeopleId(acl.getRelationpeopleid());
				List<Lcbank> lcBankList = this.LcBankDao.findModel("cid = "
						+ acl.getRelationpeopleid() + " and type = "
						+ acl.getRelationpeopletype());
				if (lcBankList != null) {
					cepb.setBasicBank(lcBankList.get(0).getSaveaccount());
				}
				cepb.setRelationpeopletype(acl.getRelationpeopletype());
				if (acl.getRelationpeopletype() == 1) {
					Personalbasic personalbasic = this.personalbasicDao
							.findOne("id", acl.getRelationpeopleid());
					if (personalbasic != null) {
						cepb.setPeopleName(personalbasic.getName());
					}
				} else if (acl.getRelationpeopletype() == 2) {
					Companybasic companybasic = this.companybasicDao.findOne(
							"id", acl.getRelationpeopleid());
					if (companybasic != null) {
						cepb.setPeopleName(companybasic.getName());
					}
				}
				list.add(cepb);
			}
		}
		return list;
	}

	@Override
	public List<?> getAllHypothecateList(Integer applyid) {
		List<CreditHypothecateThingBean> list = new ArrayList<CreditHypothecateThingBean>();
		List<Applycustomrelation> lists = this.applycustomrelationDao
				.findModel("applyid = " + applyid + " and relationtype = 1");
		if (lists != null) {
			for (Applycustomrelation acl : lists) {
				CreditHypothecateThingBean ahtb = new CreditHypothecateThingBean();
				ahtb.setRelationpeopletype(acl.getRelationpeopletype());
				ahtb.setId(acl.getThingsid());

				List<Companypledger> li = this.companypledgerDao
						.findModel("typerelation = 1 and typecustom = "
								+ acl.getRelationpeopletype());
				if (li != null) {
					Companypledger companypledger = li.get(0);
					ahtb.setThingName(companypledger.getCollateralname());
					if (companypledger.getCollateralvalueidentification() == null) {
						ahtb.setPrice(0);
					} else {
						ahtb.setPrice(companypledger
								.getCollateralvalueidentification());
					}
				}

				ahtb.setPeopleid(acl.getRelationpeopleid());

				if (acl.getRelationpeopletype() == 1) {
					Personalbasic personalbasic = this.personalbasicDao
							.findOne("id", acl.getRelationpeopleid());
					if (personalbasic != null) {
						ahtb.setPeopleName(personalbasic.getName());
					}
				} else if (acl.getRelationpeopletype() == 2) {
					Companybasic companybasic = this.companybasicDao.findOne(
							"id", acl.getRelationpeopleid());
					if (companybasic != null) {
						ahtb.setPeopleName(companybasic.getName());
					}
				}
				list.add(ahtb);
			}
		}
		return list;
	}

	@Override
	public List<?> getAllPledgeList(Integer applyid) {
		List<CreditPledgeThingBean> list = new ArrayList<CreditPledgeThingBean>();
		List<Applycustomrelation> lists = this.applycustomrelationDao
				.findModel("applyid = " + applyid + " and relationtype = 2");
		if (lists != null) {
			for (Applycustomrelation acl : lists) {
				CreditPledgeThingBean ahtb = new CreditPledgeThingBean();
				ahtb.setRelationpeopletype(acl.getRelationpeopletype());
				ahtb.setId(acl.getThingsid());

				List<Companypledger> li = this.companypledgerDao
						.findModel("typerelation = 2 and typecustom = "
								+ acl.getRelationpeopletype());
				if (li != null) {
					Companypledger companypledger = li.get(0);
					ahtb.setThingName(companypledger.getCollateralname());
					if (companypledger.getCollateralvalueidentification() == null) {
						ahtb.setPrice(0);
					} else {
						ahtb.setPrice(companypledger
								.getCollateralvalueidentification());
					}
				}

				ahtb.setPeopleid(acl.getRelationpeopleid());

				if (acl.getRelationpeopletype() == 1) {
					Personalbasic personalbasic = this.personalbasicDao
							.findOne("id", acl.getRelationpeopleid());
					if (personalbasic != null) {
						ahtb.setPeopleName(personalbasic.getName());
					}
				} else if (acl.getRelationpeopletype() == 2) {
					Companybasic companybasic = this.companybasicDao.findOne(
							"id", acl.getRelationpeopleid());
					if (companybasic != null) {
						ahtb.setPeopleName(companybasic.getName());
					}
				}
				list.add(ahtb);
			}
		}
		return list;
	}

}
